package com.lanou3g.code0423;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/4/23;
 * Otherwise, I don't know who created it either
 */

public class SendClient {
    public static void main(String[] args) {
        try {
            // 暂时不需要接收数据,所以随机绑定一个端口号即可
            DatagramSocket ds = new DatagramSocket();

            String s = "hello World";
            byte[] buf = s.getBytes();// 数据数组
            System.out.println(buf.length);
            DatagramPacket dp = new DatagramPacket(buf,
                    buf.length,
                    //要发送到的目的地 地址,端口号
                    new InetSocketAddress("255.255.255.255",
                            7788));
            ds.send(dp); // 发送

            byte[] bytes = new byte[1024];
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
            ds.receive(packet);// 等着接收数据
            String data = new String(packet.getData(),0,
                    packet.getLength());
            System.out.println(data);// 显示数据
            ds.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
